<template>
    <el-row :gutter="20" class="mainDiv" style="padding: 10px;margin: 0px; box-sizing: border-box;">
        <div class="rowHead">
                <el-select v-model="page.cid" placeholder="请选择分类" @change="getmsg">
                    <el-option label="请选择项目所属分类" value="0">
                    </el-option>
                    <el-option v-for="item in categorylist" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>

                <el-input style="width:300px" @keyup.enter.native="getmsg" placeholder="请输入项目编号，名称" v-model="page.search">
                    <i slot="suffix" class="el-icon-search" style="line-height: 40px;width: 25px;cursor: pointer;" @click="getmsg"></i>
                </el-input>     
        </div>
        <el-table :data="goodslist" height="100%" @row-click="selectRow" :row-class-name="tableRowClassName">
            <el-table-column prop="selected" label="选择">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.selected"></el-switch>
                </template>
            </el-table-column>
            <el-table-column prop="name" label="名称"></el-table-column>
            <el-table-column prop="number" label="编号"></el-table-column>
            <el-table-column prop="price" label="价格"></el-table-column>
            <el-table-column prop="usetime" label="用时"></el-table-column>
            <el-table-column prop="remarks" label="描述" :show-overflow-tooltip="true"></el-table-column>
        </el-table>
        <div class="rowHead">
            <el-pagination @current-change="changecurrent" :page-size="page.limits" :current-page="page.pages" layout="total, prev, pager, next" :total="total">
            </el-pagination>
            <el-button type="success" size="mini" icon="check" style="float: right;margin-left: 10px" @click="goback">确定选择</el-button>
            <el-button type="danger" size="mini" icon="close" style="float: right" @click="closeWindow">取消关闭</el-button>
        </div>
    </el-row>
</template>
<script>
import C from '@/common/common.js'

export default {
  name: 'selectSubject',
  data () {
    return {
      goodslist: [],
      categorylist: [],
      selectlist: [],
      total: 0,
      page: {
        limits: 15,
        pages: 1,
        cid: '',
        search: ''
      }
    }
  },
  methods: {
    selectRow: function (row, event, column) {
      event.preventDefault()
      let key = this.inselectlist(row.id)
      if (key > -1) {
        this.selectlist.splice(key, 1)
        row.selected = false
      } else {
        this.selectlist.push(row)
        row.selected = true
      }
    },
    tableRowClassName: function (row, index) {
      if (row.selected) return 'info-row'
    },
    // 计算是否选择
    checkselect: function (data) {
      data.forEach((row, index) => {
        data[index].selected = this.inselectlist(row.id) > -1
      })
      this.goodslist = data
    },
    inselectlist: function (id) {
      let selectIndexKey = -1
      this.selectlist.forEach((row, index) => {
        if (row.id == id) {
          selectIndexKey = index
        }
      })
      return selectIndexKey
    },
    goback: function () {
      C.closeBrowserWindow(this.selectlist)
    },
    closeWindow: function () {
      C.closewin()
    },
    changecurrent: function (val) {
      this.page.pages = val
      this.getmsg()
    },
    getmsg: async function () {
      let data = await C.ajax('/shop/popup/getsubjectlist', this.page, 'post')
      if (data) {
        this.total = data.count
        this.checkselect(data.data)
      }
    }
  },
  mounted: async function () {
    // 获取品牌列表
    let data = await C.ajax('/shop/popup/getsubjectcategorylist', {}, 'post')
    if (data) {
      this.categorylist = data
    }
    this.getmsg()
  }
}
</script>